package cn.wolfcode.trip.api.controller;

import cn.wolfcode.trip.base.query.SearchQueryObject;
import cn.wolfcode.trip.base.service.INewsService;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import cn.wolfcode.trip.base.service.ITravelService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户收藏功能的查询类
 */
@RestController
@RequestMapping("/enshrines")
public class UserCollectionController {

    @Autowired
    private IStrategyDetailService strategyDetailService;//1

    @Autowired
    private ITravelService travelService;//2

    @Autowired
    private INewsService newsService;//3

    /**
     * 获取搜索类型的接口(攻略/游记/日报)
     * 1.资源 /searchs/count
     * 2.请求方式 get
     * 3.请求参数 keyword
     * 4.返回结果 json
     */
    @GetMapping("/count")
    public Map<String, Object> count(SearchQueryObject qo){
        HashMap map = new HashMap();
        //查询用户所有收藏的攻略文章数量
        int strategyDetailNum = strategyDetailService.globalNumberSearch(qo);
        map.put("strategyDetailCount",strategyDetailNum);
        //查询用户所有收藏的游记文章数量
        int travelNum = travelService.globalNumberSearch(qo);
        map.put("travelCount",strategyDetailNum);
        //查询用户所有收藏的日报文章数量
        int newsNum = newsService.globalNumberSearch(qo);
        map.put("newsCount",newsNum);
        return map;
    }

    /**
     * 返回搜素封面和内容资源的接口
     * 1.资源 /searchs
     * 2.请求方式 get
     * 3.请求参数 type keyword
     * 4.返回结果 json
     */
    @GetMapping
    public PageInfo search(SearchQueryObject qo){
        //创建map
        //Map map = new HashMap();
        //获取type类型
        Integer type = qo.getType();
        if (type != null){
            //判断type来进行分类搜索
            if (type == 1){//攻略
                return strategyDetailService.globalSearch(qo);
            } else if (type == 2){//游记
                return travelService.globalSearch(qo);
            } else  if (type == 3){//日报
                return newsService.globalSearch(qo);
            }
        }else {//没有类型传入判断
            if (strategyDetailService.globalNumberSearch(qo) > 0 ){//大于零进行攻略查询
                return strategyDetailService.globalSearch(qo);

            }else if (travelService.globalNumberSearch(qo) > 0 ){//大于零进行游记查询
                return travelService.globalSearch(qo);

            }else if (newsService.globalNumberSearch(qo) > 0 ){//大于零进行日报查询
                return newsService.globalSearch(qo);
            }
        }
        return null;
    }
}
